//
//  ACAssetCell.m
//  AlbumFetch
//
//  Created by 黄志明 on 2017/2/6.
//  Copyright © 2017年 黄志明. All rights reserved.
//

#import "ACAssetCell.h"
#import "ACAssetModel.h"
#import <Photos/Photos.h>
#import "Masonry.h"
#import "ACAssetAccessManager.h"

@interface ACAssetCell()

@property (nonatomic, strong) UIButton *tapButton;

@property (nonatomic, strong) UIImageView *assetThumbnailImageView;

// 视频文件的ICON
@property (nonatomic, strong) UIImageView *playableFileICONImageView;
@property (nonatomic, strong) UILabel *durationLabel;

@property (nonatomic, strong) ACAssetModel *assetModel;
@property (nonatomic, assign) PHImageRequestID imageRequestID;
//@property (nonatomic, strong) NSString *assetIdentifier;

@end

@implementation ACAssetCell

#pragma mark -Life Cycle

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        _cellWidth = ceil(([UIScreen mainScreen].bounds.size.width - 25) / 4);
        [self setupUI];
    }
    
    return self;
}

- (void)setupUI
{
    [self.contentView addSubview:self.assetThumbnailImageView];
    [self.assetThumbnailImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self.contentView);
    }];
    
    [self.contentView addSubview:self.tapButton];
    [self.tapButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.contentView.mas_right);
        make.top.mas_equalTo(self.contentView.mas_top);
        make.width.mas_equalTo(44);
        make.height.mas_equalTo(44);
    }];
    
    [self.contentView addSubview:self.durationLabel];
    [self.durationLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.contentView).offset(-3);
        
    }];
    
    [self.contentView addSubview:self.playableFileICONImageView];
    [self.playableFileICONImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.contentView).offset(5);
        make.bottom.mas_equalTo(self.contentView).offset(-5);
        make.width.mas_equalTo(10);
        make.height.mas_equalTo(10);
    }];
}

- (void)bindImage:(UIImage *)image asset:(ALAsset *)asset
{
    self.assetThumbnailImageView.image = image;
}

- (void)bindData:(ACAssetModel *)assetModel
{
    if (_imageRequestID)
    {
        [[ACAssetAccessManager sharedManager] cancelFetchReqeust:_imageRequestID];
    }
    
    if (assetModel.modelType == ACAssetModelTypePhoto)
    {
        self.playableFileICONImageView.hidden = YES;
    }
    else if (assetModel.modelType == ACAssetModelTypeVideo)
    {
        self.playableFileICONImageView.hidden = NO;
    }
    
    self.assetThumbnailImageView.image = nil;
    
    _assetModel = assetModel;
//    _assetIdentifier = _assetModel.localIdentifier;
    
    if (_assetModel.modelType == ACAssetModelTypeVideo ||
        _assetModel.modelType == ACAssetModelTypeAudio)
    {
        self.durationLabel.text = [NSString stringWithFormat:@"%02zd", assetModel.duration];
    }
    else
    {
        self.durationLabel.text = @"";
    }
    
    CGSize cropSize = CGSizeMake(_cellWidth, _cellWidth);
    __weak typeof(self) weakSelf = self;
    _imageRequestID = [[ACAssetAccessManager sharedManager] fetchImageInAsset:assetModel.asset
                                                                         size:cropSize
                                                                   completion:^(UIImage *photo, NSDictionary *info, BOOL isDegraded) {
                                                                       
                                                                       weakSelf.assetThumbnailImageView.image = photo;
                                                                       
                                                                   }];
    
}

- (void)changeSelectStatus:(BOOL)isSelected
{
    self.tapButton.selected = isSelected;
}

#pragma mark - Private method

- (void)tapButtonClicked:(id)sender
{
    if (_delegate)
    {
        if (self.tapButton.selected)
        {
            if ([_delegate respondsToSelector:@selector(didDeselectCell:)])
            {
                [_delegate didDeselectCell:self];
            }
        }
        else
        {
            if ([_delegate respondsToSelector:@selector(shouldSelectCell:)])
            {
                if ([_delegate shouldSelectCell:self] &&
                    [_delegate respondsToSelector:@selector(didSelectCell:)])
                {
                    [_delegate didSelectCell:self];
                }
                else
                {
                    return;
                }
            }
            else
            {
                if ([_delegate respondsToSelector:@selector(didSelectCell:)])
                {
                    [_delegate didSelectCell:self];
                }
            }
        }
    }
    
    self.tapButton.selected = !self.tapButton.selected;
}

#pragma mark - Getter

- (UIButton *)tapButton
{
    if (!_tapButton)
    {
        _tapButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _tapButton.imageView.contentMode = UIViewContentModeScaleAspectFit;
        [_tapButton setContentHorizontalAlignment:UIControlContentHorizontalAlignmentFill];
        [_tapButton setContentVerticalAlignment:UIControlContentVerticalAlignmentFill];
        [_tapButton setImage:[UIImage imageNamed:@"AssetPicker.bundle/btn_pic_selected"] forState:UIControlStateNormal];
        [_tapButton setImage:[UIImage imageNamed:@"AssetPicker.bundle/btn_pic_pressed"] forState:UIControlStateSelected];
        [_tapButton addTarget:self action:@selector(tapButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
    }
    
    return _tapButton;
}

- (UIImageView *)assetThumbnailImageView
{
    if (!_assetThumbnailImageView)
    {
        _assetThumbnailImageView = [[UIImageView alloc] init];
        _assetThumbnailImageView.contentMode = UIViewContentModeScaleAspectFill;
        _assetThumbnailImageView.clipsToBounds = YES;
    }
    
    return _assetThumbnailImageView;
}

- (UIImageView *)playableFileICONImageView
{
    if (!_playableFileICONImageView)
    {
        _playableFileICONImageView = [[UIImageView alloc] init];
        _playableFileICONImageView.backgroundColor = [UIColor whiteColor];
        _playableFileICONImageView.hidden = YES;
    }
    
    return _playableFileICONImageView;
}

- (UILabel *)durationLabel
{
    if (_durationLabel)
    {
        _durationLabel = [[UILabel alloc] init];
        _durationLabel.font = [UIFont systemFontOfSize:12];
        _durationLabel.textColor = [UIColor whiteColor];
        _durationLabel.textAlignment = NSTextAlignmentRight;
        _durationLabel.hidden = YES;
    }
    
    return _durationLabel;
}


@end
